/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 055.c
 * duty of doctors
 ***********************************************************************************************/

#include <stdio.h>
#include <string.h>

/*
 * in this program, i will test a method to generate a 7-digit 7-to-carry system in order to
 * present 7 doctor in 7 days, and 7 digits can not be same in the same time.
 * it can also be used when different digit with different number-to-carry.
 *
 * in particular, N-digit N-to-carry system with non-same digits requirement can be also
 * looked as N full arrangement, and that's more efficient.
 */

#define DOC_A 0
#define DOC_B 1
#define DOC_C 2
#define DOC_D 3
#define DOC_E 4
#define DOC_F 5
#define DOC_G 6

#define MON 0
#define TUE 1
#define WED 2
#define THU 3
#define FRI 4
#define SAT 5
#define SUN 6

#define N2C 7
#define DIGITS 7

int add1(int* a)
{
	int i = 0;
	for (i = 0; i < DIGITS; i++)
	{
		a[i]++;
		if (a[i] == N2C)
			a[i] = 0;
		else
			return(1);
	}
	return(0);
}

int main()
{
	int duty[N2C];
	int doctor[DIGITS];
	memset(doctor, 0, sizeof(int)*DIGITS);

	while (1)
	{
		// check same digit
		int i = 0, flag = 1;
		memset(duty, 0, sizeof(int)*N2C);
		for (i = 0; i < DIGITS; i++)
		{
			if (duty[doctor[i]] == 1)
			{
				flag = 0;
				break;
			}
			else
			{
				duty[doctor[i]] = 1;
			}
		}

		// check relationship
		if (flag == 1)
		{
			if (
				doctor[DOC_F] == THU && 
				((doctor[DOC_B] < doctor[DOC_F] && doctor[DOC_C] > doctor[DOC_F])||
				(doctor[DOC_B] > doctor[DOC_F] && doctor[DOC_B] < doctor[DOC_F])) &&
				(doctor[DOC_A] - doctor[DOC_C] + 7) % 7 == 1 &&
				(doctor[DOC_D] - doctor[DOC_E] + 7) % 7 == 2 &&
				(doctor[DOC_G] - doctor[DOC_B] + 7) % 7 == 3
				)
			{
				int j = 0;
				for (j = 0; j < DIGITS; j++)
					printf("%3d", doctor[j]);
				printf("\n");
			}
		}

		if (add1(doctor) == 0) break;
	}
}

